﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Sharp.Platform.Mvc.Bootstrap
{
    /// <summary>
    /// 输入组件扩展程序。
    /// </summary>
    public static class InputExtensions
    {
        /// <summary>
        /// 文本框
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static InputBuilder TextBox(this BootstrapFactory factory)
        {
            return new InputBuilder().Type().Text();
        }
        /// <summary>
        /// 文本框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="factory"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static InputBuilder TextBoxFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.TextBox(), expression);
        }
        /// <summary>
        /// 密码框
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static InputBuilder Password(this BootstrapFactory factory)
        {
            return new InputBuilder().Type().Password();
        }
        /// <summary>
        /// 密码框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="factory"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static InputBuilder PasswordFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.Password(), expression);
        }
        /// <summary>
        /// 复选框
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static InputBuilder CheckBox(this BootstrapFactory factory)
        {
            return new InputBuilder().Type().Checkbox();
        }
        /// <summary>
        /// 复选框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="factory"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static InputBuilder CheckBoxFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.CheckBox(), expression);
        }

        /// <summary>
        /// 拾色器
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static InputBuilder Color(this BootstrapFactory factory)
        {
            return new InputBuilder().Type().Color();
        }
        /// <summary>
        /// 日历控件
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static InputBuilder Date(this BootstrapFactory factory)
        {
            return new InputBuilder().Type().Date();
        }

        public static InputBuilder DateFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.Date(), expression);
        }



        #region Private Method

        private static InputBuilder UnobtrusiveValidationAttributes<TModel, TProperty>(this BootstrapFactory<TModel> factory, InputBuilder builder, Expression<Func<TModel, TProperty>> expression)
        {
            var htmlHelper = factory.HtmlHelper;

            var name = ExpressionHelper.GetExpressionText(expression);

            var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var attributes = htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata);

            var value = htmlHelper.FormatValue(modelMetadata.Model, null);
            builder.Id(fullHtmlFieldName).Name(fullHtmlFieldName).Value(value);

            if (!modelMetadata.Watermark.IsNullOrEmpty())
            {
                builder.Placeholder(modelMetadata.Watermark);
            }
            if (modelMetadata.DataTypeName == "MultilineText")
            {
                //builder
            }

            foreach (var attribute in attributes)
            {
                builder.Attribute(attribute.Key, attribute.Value.ToString(), true);
            }

            return builder;
        }

        #endregion Private Method
    }
}